
(function($){

var getPath = function(me){
		return me.find(">input[type=hidden]:eq(0)").val();
	},

	getPathSelector = function(me, extra){
		return me.find(">fieldset:eq(0) "+(extra?extra:""));
	},

	deleteItem = function(e){
		$(this).one("storagedelete", function(e){ $(this).parent().fadeOut();});
		$(this).jsonRequest("MgmStorage", {action:"delete",
												path: e.data},"storagedelete" );
	},

	showTab = function(item, me){
		var file = me.find(">div:eq(0)"),
			type = file.find(">span:eq(0)"),
			name = file.find(">span:eq(1)"),
			preview = file.find(">fieldset>div:eq(0)"),
			folder = me.find(">div:eq(1)"),
			upload = folder.find(">div.request:eq(0)"),
			list = folder.find(">ul"),
			upload_path = upload.find(">input[name=path]"),
			list_item,x;

		if ( item.type == "folder" ){
			file.slideUp();
			list.empty();
			upload_path.val( item.path );
			for (x in item.leaves){
				list.append( $('<li></li>')
								.append($("<button style='display: inline;'></button>")
									.button({icons:{primary:"ui-icon-close"},text:false})
									.bind("click", item.path+"/"+item.leaves[x].name, deleteItem) ) )
					.append( item.leaves[x].name );
			}
			
			folder.slideDown();

		}else{
			folder.slideUp();
			type.removeClass();
			type.addClass("ui-icon");
			type.addClass( item.type_icon );
			name.html( item.name );
			preview.empty();
			preview.append( $("<a href='"+item.full_path+"'>...</a>") );
			file.slideDown();
		}

	},

	loadPath = function( path_data, params ){
		var path_item,select_item,
			leaves = {};
		$.each( path_data.leaves, function(v,t){
						leaves[v] = t.type+" - "+t.name;
					});
		path_item = $("<span><select></select></span>").attr("path", params.path);
		select_item = path_item.find(">select")
							.bind("change", params.me, pathChange)
							.empty()
							.addOption("", "/")
							.addOptions( leaves )
							.addOption( "create_new_folder", "Create new folder here...")
							.selectValue( params.file );
		getPathSelector(params.me).append( path_item );
		if ( params.file && 
			(path_data["leaves"][params.file]) &&
			(path_data["leaves"][params.file]["type"] == "folder") ) {

			params.path += "/" + params.file;
			params.file = params.build ? params.build.shift() : false;
			Core.Storage( params.path, {cb: loadPath, data:params}, true );
		}
		else{
			Core.Storage( params.full_path, {cb: showTab, data:params.me}, true );
		}
	},

	/**
	* This entry point starts the rebuild of the path combo boxes
	*/
	buildPath = function(me){
		var split_path = getPath(me).split("/");
		getPathSelector(me, ">span").remove();
		Core.Storage( "/", {cb: loadPath,
							data:{	me:me,
									path: split_path.shift(),
									file: split_path.shift(),
									full_path: getPath(me),
									build: split_path}}, true );
	},

	pathChange = function(e){
		var me =e.data,new_path,
			path = $(this).parent().attr("path"),
			file = $(this).find("option:selected").val();
		if ( file == "create_new_folder" ){
			new_path = prompt("New folder name: ", "");
			if ( new_path ){
				$(this).one("newcreated", me,function(e){buildPath(e.data);});
				$(this).jsonRequest("MgmStorage", {action:"create_folder",
													path: path,
													new_path: new_path},"newcreated" );
			}
		}else{
			me.find(">input[type=hidden]:eq(0)").val(path+"/"+file);
			buildPath(me);
		}
	};

/**
* Initialize storage objects
*/
Core.ObjectTypes["div.storage"] = function(){
	var me=$(this);
	buildPath(me);
};

}(jQuery));